###### THIS FILE TAKES THE CLEANED STATA FILES TO CREATE THE FIGURES ######

##### Importing libraries 

library(rdrobust)
library(haven)
library(dplyr)
library(ggplot2)
library(rdd)
library(tidyr)
library(cowplot)
library(mediation)
library(stargazer)


#### STUDY 1  WHITE RESPONDENTSS, BLACK/WHITE CANDIDATES ####

setwd("./Study1")
data <- read_dta("study1_cleaned.dta")

setwd("../figures")

data <- subset(data, is.na(treat)!="TRUE")

d.i1<-data %>%
  group_by(treat) %>%
  dplyr::summarize(avg=mean(index, na.rm=TRUE), n=n(), sd=sd(index, na.rm=TRUE), se=sd/sqrt(n))

d.i1$statement = as.factor(c(0,1,0,1))
d.i1$race = as.factor(c(0,0,1,1))
pd <- position_dodge(0.1) # move them .05 to the left and right

i1 <- ggplot(data=d.i1, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  geom_text(aes(label = paste(round(avg,2))), position = position_dodge(0.75),
            show.legend = FALSE)+
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Approval", x = "Candidate Identity", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw() +theme(legend.position = "none") +  theme(legend.position = c(0.8, 0.15),
                                                       legend.spacing.y = unit(0, "mm"),
                                                       panel.border = element_rect(colour = "black", fill=NA),
                                                       legend.background = element_blank(),
                                                       #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                                                       legend.box.background = element_rect(colour = "black"))


i1
ggsave(file="figure1_study1.pdf", width = 100, height = 100, units = "mm", dpi=1200)




###RR Interaction ##
r1<- ggplot(data) + 
  aes(y =index, x = symrace, color=as.factor(treat),linetype=as.factor(treat))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Racial Resentment", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  coord_cartesian(ylim=c(-.1,.9)) +
  theme_bw() + theme(legend.position = c(0.75, 0.2),
                     legend.spacing.y = unit(0, "mm"),
                     panel.border = element_rect(colour = "black", fill=NA),
                     legend.background = element_blank(),
                     #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                     legend.box.background = element_rect(colour = "black"))

r1
ggsave(file="figure2_study1.pdf", width = 100, height = 100, units = "mm", dpi=1200)




####STUDY 1: Appendix Plots



### Character ###

d.c1<-data %>%
  group_by(treat) %>%
  dplyr::summarize(avg=mean(indexChar, na.rm=TRUE), n=n(), sd=sd(indexChar, na.rm=TRUE), se=sd/sqrt(n))
d.c1$statement = as.factor(c(0,1,0,1))
d.c1$race = as.factor(c(0,0,1,1))

c1 <- ggplot(data=d.c1, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Character", x = "Candidate Identity", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw()  + theme(legend.position = "none")

c1
ggsave(file="appendix_study1_character.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### Appropriateness ###

d.a1<-data %>%
  group_by(treat) %>%
  dplyr::summarize(avg=mean((indexApp2), na.rm=TRUE), n=n(), sd=sd((indexApp2), na.rm=TRUE), se=sd/sqrt(n))
d.a1$statement = as.factor(c(0,1,0,1))
d.a1$race = as.factor(c(0,0,1,1))

a1 <- ggplot(data=d.a1, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Appropriateness", x = "Candidate Identity", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.15,.85)) +
  theme_bw()  + theme(legend.position = "none")

a1
ggsave(file="appendix_study1_appropriate.pdf", width = 100, height = 100, units = "mm", dpi=1200)


##Partisan Perceptions 
d.p1<-data %>%
  group_by(treat) %>%
  dplyr::summarize(avg=mean(perceived_Dem, na.rm=TRUE), n=n(), sd=sd(perceived_Dem, na.rm=TRUE), se=sd/sqrt(n))

d.p1$statement = as.factor(c(0,1,0,1))
d.p1$race = as.factor(c(0,0,1,1))
pd <- position_dodge(0.1) # move them .05 to the left and right

p1 <- ggplot(data=d.p1, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Perception Candidate is a Democrat", x = "Candidate Identity", color = "Candidate Identity") +
  coord_cartesian(ylim=c(0,1)) +
  theme_bw() +theme(legend.position = "none")

p1
ggsave(file="appendix_study1_partisan.pdf", width = 100, height = 100, units = "mm", dpi=1200)



###PID Interaction
ppid1<- ggplot(data) + 
  aes(y =perceived_Dem, x = pid7R, color=as.factor(treat),linetype=as.factor(treat))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Perceived as Democrat",x = expression("Party Identification D" %->% "R"), color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  coord_cartesian(ylim=c(0,1)) +
  theme_bw()

ppid1

ggsave(file="appendix_study1_partisaninteraction.pdf", width = 150, height = 100, units = "mm", dpi=1200)


### BLACK POLITICAL INFLUENCE

inf <- ggplot(data) +
  aes(y = index, x = data$inf_black, color=as.factor(treat),linetype=as.factor(treat))+
  geom_smooth(method = "lm") +
  labs( y= "Candidate Approval",x = expression("Blacks Influence Politics (Too Little" %->% "Too Much)"), color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) +
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) +
  #scale_x_discrete(breaks=c("-1","0","1"), labels=c("Too little","About right", "Too Much")  +
  coord_cartesian(ylim=c(-.1,1.1)) +
  theme_bw() +  theme(legend.position = c(0.2125, 0.8),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

inf
ggsave("appendix_study1_black_inf.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### BLACK DISCRIMINATION


dis <- ggplot(data) + 
  aes(y = vote, x = (discrim_blacks), color=as.factor(treat),linetype=as.factor(treat))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Perceived Discrimination Against Blacks", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  coord_cartesian(ylim=c(-.1,1.1)) +
  theme_bw() +  theme(legend.position = c(0.7, 0.8),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

dis
ggsave("appendix_study1_disrimination.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### FEELING THERMOMETER TOWARD BLACKS

ft<- ggplot(data) + 
  aes(y = vote, x = (FT_blacks), color=as.factor(treat),linetype=as.factor(treat))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Feeling Thermometer toward Blacks", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Black", "Derogating Black")) + 
  coord_cartesian(ylim=c(-.1,1.1)) +
  theme_bw()  +  theme(legend.position = c(0.7, 0.8),
                       legend.spacing.y = unit(0, "mm"),
                       panel.border = element_rect(colour = "black", fill=NA),
                       legend.background = element_blank(),
                       #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                       legend.box.background = element_rect(colour = "black"))

ft
ggsave("appendix_study1_thermometer.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### P-values Reported in Text, and some referenced in the text ###
t.test(data$indexA[data$treat2==1],data$indexA[data$treat2==2])
t.test(data$indexA[data$treat2==1],data$indexA[data$treat2==3])
t.test(data$indexA[data$treat2==3],data$indexA[data$treat2==4])
t.test(data$indexA[data$treat2==2],data$indexA[data$treat2==4])
t.test(data$index[data$treat2==1],data$index[data$treat2==2])
t.test(data$index[data$treat2==1],data$index[data$treat2==3])
t.test(data$index[data$treat2==3],data$index[data$treat2==4])
t.test(data$index[data$treat2==2],data$index[data$treat2==4])

t.test(data$indexApp[data$treat2==1],data$indexApp[data$treat2==2])
t.test(data$indexApp[data$treat2==1],data$indexApp[data$treat2==3])
t.test(data$indexApp[data$treat2==3],data$indexApp[data$treat2==4])
t.test(data$indexApp[data$treat2==2],data$indexApp[data$treat2==4])

t.test((data$indexA[data$treat2==2]-data$indexA[data$treat2==1]),
       (data$indexA[data$treat2==4]-data$indexA[data$treat2==3]))


B_der <- lm(index ~ treat2, data=data)




#### STUDY 2: WHITE RESPONDENTS, MUSLIM/WHITE CANDIDATES ####

setwd("../Study2")
data <- read_dta("study2_cleaned.dta")
setwd("../figures")

#### FIGURE 1B: STUDY 2 OUTPUT ###

d.i2<-data %>%
  group_by(treatment) %>%
  dplyr::summarize(avg=mean(WM_index, na.rm=TRUE), n=n(), sd=sd(WM_index, na.rm=TRUE), se=sd/sqrt(n))

d.i2$statement = as.factor(c(0,1,0,1))
d.i2$race = as.factor(c(0,0,1,1))

i2 <- ggplot(data=d.i2, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  geom_text(aes(label = paste(round(avg,2))), position = position_dodge(0.75),
            show.legend = FALSE)+
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Statement") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Approval", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
coord_cartesian(ylim=c(.2,.8)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

i2
ggsave(file="figure1_study2.pdf", width = 100, height = 100, units = "mm", dpi=1200)


###MAR Interaction ##
r2<- ggplot(data) + 
  aes(y = WM_index, x = Postmar, color=as.factor(treatment),linetype=as.factor(treatment))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval" ,x = "Muslim American Resentment", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.1,.9)) +
  theme_bw() +  theme(legend.position = c(0.7, 0.2),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))


r2
ggsave(file="figure3_study2.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### STUDY 2 APPENDIX ###


### CHARACTER ###

d.c2<-data %>%
  group_by(treatment) %>%
  dplyr::summarize(avg=mean(WM_indexChar, na.rm=TRUE), n=n(), sd=sd(WM_indexChar, na.rm=TRUE), se=sd/sqrt(n))

d.c2$statement = as.factor(c(0,1,0,1))
d.c2$race = as.factor(c(0,0,1,1))

c2 <- ggplot(data=d.c2, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Statement") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Character", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

c2
ggsave(file="appendix_study2_character.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### APPROPRIATENESS ###

d.a2<-data %>%
  group_by(treatment) %>%
  dplyr::summarize(avg=mean(WM_indexApp, na.rm=TRUE), n=n(), sd=sd(WM_indexApp, na.rm=TRUE), se=sd/sqrt(n))

d.a2$statement = as.factor(c(0,1,0,1))
d.a2$race = as.factor(c(0,0,1,1))

a2 <- ggplot(data=d.a2, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Appropriateness", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.15,.85)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

a2
ggsave(file="appendix_study2_appropriate.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### PARTISANSHIP ###

d.p2<-data %>%
  group_by(treatment) %>%
  dplyr::summarize(avg=mean(cand_democrat, na.rm=TRUE), n=n(), sd=sd(cand_democrat, na.rm=TRUE), se=sd/sqrt(n))

d.p2$statement = as.factor(c(0,1,0,1))
d.p2$race = as.factor(c(0,0,1,1))

p2 <- ggplot(data=d.p2, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Statement") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Perceived as Democrat ", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(0,1)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

p2
ggsave(file="appendix_study2_partisan.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### PID Interaction ###

partisanship2 <-  ggplot(data) + 
  aes(y = cand_democrat, x = pid7R, color=as.factor(treatment),linetype=as.factor(treatment))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Perceived as Democrat" ,x = expression("Party Identification (D" %->% "R)"), color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(0,1)) +
  theme_bw() 


partisanship2
ggsave(file="appendix_study2_partisansinteraction.pdf", width = 150, height = 100, units = "mm", dpi=1200)


### Dehumanization ###


data$muslim_hum <- data$v402/100

humanization2 <-  ggplot(data) + 
  aes(y = WM_index, x = muslim_hum, color=as.factor(treatment),linetype=as.factor(treatment))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval" ,x = "Muslim American Dehumanization", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.1,.9)) +
  theme_bw() +  theme(legend.position = c(0.7, 0.2),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))


humanization
ggsave(file="appendix_study2_dehumanization.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### T-TESTS ###

t.test(data$WM_index[data$treatment==1],data$WM_index[data$treatment==2])
t.test(data$WM_index[data$treatment==1],data$WM_index[data$treatment==3])
t.test(data$WM_index[data$treatment==3],data$WM_index[data$treatment==4])
t.test(data$WM_index[data$treatment==2],data$WM_index[data$treatment==4])

t.test(data$bigoted[data$treatment==1],data$bigoted[data$treatment==2])
t.test(data$bigoted[data$treatment==1],data$bigoted[data$treatment==3])
t.test(data$bigoted[data$treatment==3],data$bigoted[data$treatment==4])
t.test(data$bigoted[data$treatment==2],data$bigoted[data$treatment==4])


t.test(data$WM_indexApp[data$treatment==1],data$WM_indexApp[data$treatment==2])
t.test(data$WM_indexApp[data$treatment==1],data$WM_indexApp[data$treatment==3])
t.test(data$WM_indexApp[data$treatment==3],data$WM_indexApp[data$treatment==4])
t.test(data$WM_indexApp[data$treatment==2],data$WM_indexApp[data$treatment==4])


#### STUDY 3: BLACK SAMPLE, WHITE/BLACK CANDIDATES ####

setwd("../Study3_4")
data <- read_dta("study3_cleaned.dta")
setwd("../Figures")


d.i3<-data %>%
  group_by(treat1) %>%
  dplyr::summarize(avg=mean(B_index, na.rm=TRUE), n=n(), sd=sd(B_index, na.rm=TRUE), se=sd/sqrt(n))


d.i3$statement = as.factor(c(0,1,0,1))
d.i3$race = as.factor(c(0,0,1,1))

i3 <- ggplot(data=d.i3, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  geom_text(aes(label = paste(round(avg,2))), position = position_dodge(0.75),
            show.legend = FALSE)+
  
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Approval", x = "Candidate Identity", color = "Statement") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw()  +  theme(legend.position = c(0.8, 0.15),
                       legend.spacing.y = unit(0, "mm"),
                       panel.border = element_rect(colour = "black", fill=NA),
                       legend.background = element_blank(),
                       #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                       legend.box.background = element_rect(colour = "black"))

i3
ggsave(file="figure1_study3.pdf", width = 100, height = 100, units = "mm", dpi=1200)



###Figure X: Linked Fate


lf3<- ggplot(data) + 
  aes(y = B_vote, x = linked_fateR, color=as.factor(treat1),linetype=as.factor(treat1))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Linked Fate", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","longdash"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.21,.9)) +
  theme_bw()   +  theme(legend.position = c(0.68, 0.18),
                        legend.spacing.y = unit(0, "mm"),
                        panel.border = element_rect(colour = "black", fill=NA),
                        legend.background = element_blank(),
                        #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 10),
                        legend.box.background = element_rect(colour = "black"))


lf3 
ggsave(file="figure4_study3.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### APPENDIX ###

### Character

d.c3<-data %>%
  group_by(treat1) %>%
  dplyr::summarize(avg=mean(B_indexChar, na.rm=TRUE), n=n(), sd=sd(B_indexChar, na.rm=TRUE), se=sd/sqrt(n))


d.c3$statement = as.factor(c(0,1,0,1))
d.c3$race = as.factor(c(0,0,1,1))

c3 <- ggplot(data=d.c3, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Character", x = "Candidate Identity", color = "Statement") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw()  + theme(legend.position = "none")

c3
ggsave(file="appendix_study3_character.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### Appropriate

d.a3<-data %>%
  group_by(treat1) %>%
  dplyr::summarize(avg=mean(B_indexApp, na.rm=TRUE), n=n(), sd=sd(B_indexApp, na.rm=TRUE), se=sd/sqrt(n))


d.a3$statement = as.factor(c(0,1,0,1))
d.a3$race = as.factor(c(0,0,1,1))

a3 <- ggplot(data=d.a3, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Appropriateness", x = "Candidate Identity", color = "Statement") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  #coord_cartesian(ylim=c(.25,.85)) +
  theme_bw()  + theme(legend.position = "none")

a3 
ggsave(file="appendix_study3_appropriate.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### Partisanship

d.p3<-data %>%
  group_by(treat1) %>%
  dplyr::summarize(avg=mean(B_perceivedDem, na.rm=TRUE), n=n(), sd=sd(B_perceivedDem, na.rm=TRUE), se=sd/sqrt(n))


d.p3$statement = as.factor(c(0,1,0,1))
d.p3$race = as.factor(c(0,0,1,1))

p3 <- ggplot(data=d.p3, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Black Candidate")) +
  labs( y= "Mean Candidate Perceived as Democrat", x = "Candidate Identity", color = "Statement") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(0,1)) +
  theme_bw()  + theme(legend.position = "none")

p3
ggsave(file="appendix_study3_partisan.pdf", width = 100, height = 100, units = "mm", dpi=1200)

### Group Solidarity

r3Emp<- ggplot(data) + 
  aes(y = B_index, x = code6, color=as.factor(treat1),linetype=as.factor(treat1))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval", x = "Group Empathy", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.2,.9)) +
  theme_bw()   +  theme(legend.position = c(0.75, 0.5),
                        legend.spacing.y = unit(0, "mm"),
                        panel.border = element_rect(colour = "black", fill=NA),
                        legend.background = element_blank(),
                        #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                        legend.box.background = element_rect(colour = "black"))


r3Emp
ggsave(file="appendix_empathy_study3.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### Partisan Interaction


r3partisanship<- ggplot(data) + 
  aes(y = B_perceivedDem, x = pidR, color=as.factor(treat1),linetype=as.factor(treat1))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Perceieved as Demcorat", x =  expression("Party Identification (D" %->% "R)"), color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(0,1)) +
  theme_bw()  


r3partisanship
ggsave(file="appendix_study3_partisaninteraction.pdf", width = 150, height = 100, units = "mm", dpi=1200)



### T-test values
t.test(data$B_index[data$treat1==1],data$B_index[data$treat1==2])
t.test(data$B_index[data$treat1==1],data$B_index[data$treat1==3])
t.test(data$B_index[data$treat1==3],data$B_index[data$treat1==4])
t.test(data$B_index[data$treat1==2],data$B_index[data$treat1==4])

t.test(data$bigoted[data$treat1==1],data$bigoted[data$treat1==2])
t.test(data$bigoted[data$treat1==1],data$bigoted[data$treat1==3])
t.test(data$bigoted[data$treat1==3],data$bigoted[data$treat1==4])
t.test(data$bigoted[data$treat1==2],data$bigoted[data$treat1==4])

t.test(data$WM_indexApp[data$treat1==1],data$WM_indexApp[data$treat1==2])
t.test(data$WM_indexApp[data$treat1==1],data$WM_indexApp[data$treat1==3])
t.test(data$WM_indexApp[data$treat1==3],data$WM_indexApp[data$treat1==4])
t.test(data$WM_indexApp[data$treat1==2],data$WM_indexApp[data$treat1==4])


##### Black Sample BW Interactions

datab <- subset(data, treat2>2)
dataw <- subset(data, treat2<3)

### Racial Resentment

LF3_W <- lm(B_index~linked_fateR*treat1, data=dataw)
LF3_B <- lm(B_index~linked_fateR*treat1, data=datab)

###Partisanship

W1_out_demB <- lm(B_index~treat1*B_perceivedDem, data=datab)
W1_out_demW <- lm(B_index~treat1*B_perceivedDem, data=dataw)

W1_out_appB <- lm(B_index~treat1*B_indexApp, data=datab)
W1_out_appW <- lm(B_index~treat1*B_indexApp, data=dataw)


W1_out_bothB <- lm(B_index~treat1*B_indexApp + treat1*B_perceivedDem, data=datab)
W1_out_bothW <- lm(B_index~treat1*B_indexApp + treat1*B_perceivedDem, data=dataw)



stargazer(W1_out_demB,W1_out_demW, W1_out_appB, W1_out_appW, W1_out_bothB, W1_out_bothW,
          column.labels = c("Muslim candidate", "White candidate", "Muslim candidate", "White candidate",
                            "Muslim candidate", "White candidate"),
          covariate.labels = c("Derogation", "Democrat", "Derogation X Democrat", "Approriate",
                               "Derogation X Appropriate")
)





##### STUDY 4: BLACK RESPONDENTS, MUSLIM/WHITE CANDIDATES ######

setwd("../Study3_4")
data <- read_dta("study4_cleaned_coded.dta")
setwd("../Figures")


d.i4<-data %>%
  group_by(treat2) %>%
  dplyr::summarize(avg=mean(M_index, na.rm=TRUE), n=n(), sd=sd(M_index, na.rm=TRUE), se=sd/sqrt(n))

d.i4 <- na.omit(d.i4)
d.i4$statement = as.factor(c(0,1,0,1))
d.i4$race = as.factor(c(0,0,1,1))

i4 <- ggplot(data=d.i4, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Approval", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black")) +
  geom_text(aes(label = paste(round(avg,2))), position = position_dodge(0.75),
            show.legend = FALSE)
  


i4
ggsave(file="figure1_study4.pdf", width = 100, height = 100, units = "mm", dpi=1200)



r4LF<- ggplot(data) + 
  aes(y = M_index, x = linked_fateR, color=as.factor(treat2),linetype=as.factor(treat2))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Linked Fate", color = "Candidate Identity") +
  guides(fill=guide_legend(ncol=2)) +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.21,.9)) +
  theme_bw() +  theme(legend.position = c(0.68, 0.18),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

r4LF
ggsave(file="figure5_study4.pdf", width = 100, height = 100, units = "mm", dpi=1200)

  r4Emp<- ggplot(data) + 
    aes(y = M_index, x = code6, color=as.factor(treat2),linetype=as.factor(treat2))+
    geom_smooth(method = "lm") + 
    labs( y= "Candidate Approval", x = "Group Empathy", color = "Candidate Identity") +
    scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                          label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
    scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                       label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
    coord_cartesian(ylim=c(-.2,.9)) +
    theme_bw()  +  theme(legend.position = c(0.75, 0.5),
                         legend.spacing.y = unit(0, "mm"),
                         panel.border = element_rect(colour = "black", fill=NA),
                         legend.background = element_blank(),
                         #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                         legend.box.background = element_rect(colour = "black"))
  
  
  r4Emp
  ggsave(file="figure6_study4.pdf", width = 100, height = 100, units = "mm", dpi=1200)


r4<- ggplot(data) + 
  aes(y = M_index, x = antimuslim, color=as.factor(treat2),linetype=as.factor(treat2))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval",x = "Muslim American Resentment", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.1,.9)) + guides(fill=guide_legend(ncol=2)) + 
  theme_bw() +  theme(legend.position = c(0.75, 0.2),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

r4
ggsave(file="figure7_study4.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### Appendix ###

### Character

d.c4<-data %>%
  group_by(treat2) %>%
  dplyr::summarize(avg=mean(M_indexChar, na.rm=TRUE), n=n(), sd=sd(M_indexChar, na.rm=TRUE), se=sd/sqrt(n))
d.c4 <- na.omit(d.c4)
d.c4$statement = as.factor(c(0,1,0,1))
d.c4$race = as.factor(c(0,0,1,1))

c4 <- ggplot(data=d.c4, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Character", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.2,.8)) +
  theme_bw() +  theme(legend.position = c(0.8, 0.15),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))

c4
ggsave(file="appendix_study4_character.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### Appropriate

d.a4<-data %>%
  group_by(treat2) %>%
  dplyr::summarize(avg=mean(M_indexApp, na.rm=TRUE), n=n(), sd=sd(M_indexApp, na.rm=TRUE), se=sd/sqrt(n))

d.a4$statement = as.factor(c(0,1,0,1))
d.a4$race = as.factor(c(0,0,1,1))

a4 <- ggplot(data=d.i4, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Appropriateness", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(.15,.85)) + theme_bw() +
  theme(legend.position = c(0.8, 0.15),
           legend.spacing.y = unit(0, "mm"),
           panel.border = element_rect(colour = "black", fill=NA),
           legend.background = element_blank(),
           #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
           legend.box.background = element_rect(colour = "black"))

a4
ggsave(file="appendix_study4_appropriate.pdf", width = 100, height = 100, units = "mm", dpi=1200)


### Partisanship


d.p4<-data %>%
  group_by(treat2) %>%
  dplyr::summarize(avg=mean(M_perceivedDem, na.rm=TRUE), n=n(), sd=sd(M_perceivedDem, na.rm=TRUE), se=sd/sqrt(n))%>%
  drop_na()

d.p4$statement = as.factor(c(0,1,0,1))
d.p4$race = as.factor(c(0,0,1,1))

p4 <- ggplot(data=d.p4, aes(x=race, y=avg, colour = statement, shape=statement)) + 
  geom_errorbar(aes(ymin=avg-1.96*se, ymax=avg+1.96*se), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Statement",values=c(4,5), label=c("Neutral","Derogating")) +  
  scale_color_manual(name="Statement",values=c("grey65","black"), label=c("Neutral","Derogating")) + 
  #scale_fill_manual(name="Race Treatment") +
  geom_hline(yintercept=.5, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("0" = "White Candidate", "1" = "Muslim Candidate")) +
  labs( y= "Mean Candidate Perceived as Democrat", x = "Candidate Identity", color = "Candidate Identity") +
  #guides(colour = guide_legend(reverse=TRUE)) +
  coord_cartesian(ylim=c(0,1)) + 
  theme_bw() +  theme(legend.position = c(0.75, 0.2),
            legend.spacing.y = unit(0, "mm"),
            panel.border = element_rect(colour = "black", fill=NA),
            legend.background = element_blank(),
            #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
            legend.box.background = element_rect(colour = "black"))


p4
ggsave(file="appendix_study4_partisan.pdf", width = 100, height = 100, units = "mm", dpi=1200)




##Partiasnship Interaction

r4P<- ggplot(data) + 
  aes(y = M_perceivedDem, x = pidR, color=as.factor(treat2),linetype=as.factor(treat2))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Perceived as Democrat", x =  expression("Party Identification (D" %->% "R)"), color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(0,1)) +
  theme_bw()

r4P
ggsave(file="Black_MW_PartisanshipInt.pdf", width = 150, height = 100, units = "mm", dpi=1200)


data$code6_2 <- data$code5mentionsmuslimsspecifically

r4Solid<- ggplot(data) + 
  aes(y = M_index, x = code5mentionsmuslimsspecifically, color=as.factor(treat2),linetype=as.factor(treat2))+
  geom_smooth(method = "lm") + 
  labs( y= "Candidate Approval", x = "Group Solidarity", color = "Candidate Identity") +
  scale_linetype_manual(name="Treatment", values=c("dotdash","dotted","solid","dashed"),
                        label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  scale_color_manual(name="Treatment", values=c("grey55","black","grey65","grey40"),
                     label=c("Neutral White", "Derogating White", "Neutral Muslim", "Derogating Muslim")) + 
  coord_cartesian(ylim=c(-.2,.9)) +
  theme_bw()  #+  theme(legend.position = c(0.75, 0.5),
legend.spacing.y = unit(0, "mm"),
panel.border = element_rect(colour = "black", fill=NA),
legend.background = element_blank(),
#aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
legend.box.background = element_rect(colour = "black"))


r4Solid
ggsave(file="appendix_study4_solidarity.pdf", width = 100, height = 100, units = "mm", dpi=1200)



### T-tests


t.test(data$M_index[data$treat2==1],data$M_index[data$treat2==2])
t.test(data$M_index[data$treat2==1],data$M_index[data$treat2==3])
t.test(data$M_index[data$treat2==3],data$M_index[data$treat2==4])
t.test(data$M_index[data$treat2==2],data$M_index[data$treat2==4])


##### Black Sample BW Interactions

datab <- subset(data, treat1>2)
dataw <- subset(data, treat1<3)

### Racial Resentment

LF4_W <- lm(M_index~linked_fateR*treat2, data=dataw)
LF4_B <- lm(M_index~linked_fateR*treat2, data=datab)

###Partisanship

W1_out_demB <- lm(M_index~treat2*M_perceivedDem, data=datab)
W1_out_demW <- lm(M_index~treat2*M_perceivedDem, data=dataw)

W1_out_appB <- lm(M_index~treat2*M_indexApp, data=datab)
W1_out_appW <- lm(M_index~treat2*M_indexApp, data=dataw)


W1_out_bothB <- lm(M_index~treat2*M_indexApp + treat2*M_perceivedDem, data=datab)
W1_out_bothW <- lm(M_index~treat2*M_indexApp + treat2*M_perceivedDem, data=dataw)



stargazer(W1_out_demB,W1_out_demW, W1_out_appB, W1_out_appW, W1_out_bothB, W1_out_bothW,
          column.labels = c("Muslim candidate", "White candidate", "Black candidate", "White candidate",
                            "Muslim candidate", "White candidate"),
          covariate.labels = c("Derogation", "Democrat", "Derogation X Democrat", "Approriate",
                               "Derogation X Appropriate")
)

### Apendix RR/MAR Plots

stargazer(R1_B,R1_W,R2_B,R2_W,
          column.labels = c("Black", "White","Muslim", "White"),
          covariate.labels = c("Racial Resentment", "Derogation", "Racial Resentment X Derogation", 
                               "MAR","MAR X Derogation")
)




### Appendix LF Plots


stargazer(LF3_B,LF3_W,LF4_B,LF4_W,
          column.labels = c("Black", "White","Muslim", "White"),
          covariate.labels = c("Linked Fate", "Derogation", "Linked Fate X Derogation", 
                               "Derogation","Linked Fate X Derogation")
)


################ Differences Plots ##################
## Figure 8: Comparing White and Black responses ##


se_dif <- function(x,y,m,n) sqrt( ((x^2)/m) +((y^2)/n) )


ds.i1 <- d.i1 %>% group_by(race) %>%
         mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
         filter(!is.na(dif)) 
ds.i1$group <- "Black/White Experiments"
ds.i1$sample <- 1 


ds.i2 <- d.i2 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i2$group <- "Muslim/White Experiments"
ds.i2$sample <- 1 


ds.i3 <- d.i3 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i3$group <- "Black/White Experiments"
ds.i3$sample <- 2 


ds.i4 <- d.i4 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i4$group <- "Muslim/White Experiments"
ds.i4$sample <- 2 


ds <- bind_rows(ds.i1,ds.i2,ds.i3,ds.i4)

ggplot(data=ds, aes(x=as.factor(sample), y=dif, colour = race, shape=race)) + 
  geom_errorbar(aes(ymin=dif-1.96*sedif, ymax=dif+1.96*sedif), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  geom_text(aes(label = paste(round(dif,2))), position = position_dodge(.95),
            show.legend = FALSE)+
  scale_shape_manual(name="Derogation Type",values=c(4,5), label=c("Outgroup","Ingroup")) +  
  scale_color_manual(name="Derogation Type",values=c("grey65","black"), label=c("Outgroup","Ingroup")) + 
  geom_hline(yintercept=0, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("1" = "White Sample", "2" = "Black Sample")) +
  labs( y= "Difference in Approval \nof Neutral and Derogating Candidates", x = "Summary of Studies", color = "Candidate Identity") +
  coord_cartesian(ylim=c(-.5,.05)) +
  facet_grid(.~group) +
  theme_bw() +  theme(legend.position = c(0.125, 0.125),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))


ggsave("figure8.pdf", width = 6, height = 4, units = "in", dpi=1200)


### CHARACTER

ds.c1 <- d.c1 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.c1$group <- "Black/White Experiment"
ds.c1$sample <- 1 


ds.c2 <- d.c2 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.c2$group <- "Muslim/White Experiment"
ds.c2$sample <- 1 


ds.c3 <- d.c3 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.c3$group <- "Black/White Experiment"
ds.c3$sample <- 2 


ds.c4 <- d.c4 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.c4$group <- "Muslim/White Experiment"
ds.c4$sample <- 2 


ds <- bind_rows(ds.c1,ds.c2,ds.c3,ds.c4)

ggplot(data=ds, aes(x=as.factor(sample), y=dif, colour = race, shape=race)) + 
  geom_errorbar(aes(ymin=dif-1.96*sedif, ymax=dif+1.96*sedif), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Candidate Identity",values=c(4,5), label=c("White","Non-white")) +  
  scale_color_manual(name="Candidate Identity",values=c("grey65","black"), label=c("White","Non-white")) + 
  geom_hline(yintercept=0, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("1" = "White Sample", "2" = "Black Sample")) +
  labs( y= "Mean", x = "Difference between Neutral and Derogating Candidates", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.1,-.5)) +
  facet_grid(.~group) +
  theme_bw()

ggsave("appendix_character.jpg", width = 6, height = 4, units = "in", dpi=600)


### APPROPRIATENESS 

se_dif <- function(x,y,m,n) sqrt((2*((x^2+y^2)/2))/((m+n)/2))

ds.a1 <- d.a1 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.a1$group <- "Black/White Experiment"
ds.a1$sample <- 1 


ds.a2 <- d.a2 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.a2$group <- "Muslim/White Experiment"
ds.a2$sample <- 1 


ds.a3 <- d.a3 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.a3$group <- "Black/White Experiment"
ds.a3$sample <- 2 


ds.a4 <- d.a4 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(se),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.a4$group <- "Muslim/White Experiment"
ds.a4$sample <- 2 


ds <- bind_rows(ds.a1,ds.a2,ds.a3,ds.a4)

ggplot(data=ds, aes(x=as.factor(sample), y=dif, colour = race, shape=race)) + 
  geom_errorbar(aes(ymin=dif-1.96*sedif, ymax=dif+1.96*sedif), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Candidate Identity",values=c(4,5), label=c("White","Non-white")) +  
  scale_color_manual(name="Candidate Identity",values=c("grey65","black"), label=c("White","Non-white")) + 
  geom_hline(yintercept=0, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("1" = "White Sample", "2" = "Black Sample")) +
  labs( y= "Mean", x = "Difference between Neutral and Derogating Candidates", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.1,-.5)) +
  facet_grid(.~group) +
  theme_bw()

ggsave("appendix_appropriate.jpg", width = 6, height = 4, units = "in", dpi=600)





ds.i1 <- d.a1 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i1$group <- "Black/White Experiments"
ds.i1$sample <- 1 


ds.i2 <- d.a2 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i2$group <- "Muslim/White Experiments"
ds.i2$sample <- 1 


ds.i3 <- d.a3 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i3$group <- "Black/White Experiments"
ds.i3$sample <- 2 


ds.i4 <- d.a4 %>% group_by(race) %>%
  mutate(dif=avg-lag(avg), sedif = se_dif(sd,lag(sd),n,lag(n))) %>%
  filter(!is.na(dif)) 
ds.i4$group <- "Muslim/White Experiments"
ds.i4$sample <- 2 


ds <- bind_rows(ds.i1,ds.i2,ds.i3,ds.i4)

ggplot(data=ds, aes(x=as.factor(sample), y=dif, colour = race, shape=race)) + 
  geom_errorbar(aes(ymin=dif-1.96*sedif, ymax=dif+1.96*sedif), color="black", width=.1, position=pd) + 
  geom_point(size= 3, position = pd ) + 
  scale_shape_manual(name="Candidate Identity",values=c(4,5), label=c("White","Non-white")) +  
  scale_color_manual(name="Candidate Identity",values=c("grey65","black"), label=c("White","Non-white")) + 
  geom_hline(yintercept=0, linetype="longdash", size=0.5, color='grey70') +
  scale_x_discrete(labels=c("1" = "White Sample", "2" = "Black Sample")) +
  labs( y= "Mean", x = "Difference in Appropriateness between Neutral and Derogating Candidates", color = "Candidate Identity") +
  coord_cartesian(ylim=c(.05,-.5)) +
  facet_grid(.~group) +
  theme_bw() +  theme(legend.position = c(0.825, 0.125),
                      legend.spacing.y = unit(0, "mm"),
                      panel.border = element_rect(colour = "black", fill=NA),
                      legend.background = element_blank(),
                      #aspect.ratio = 1, axis.text = element_text(colour = 1, size = 12),
                      legend.box.background = element_rect(colour = "black"))


ggsave("DifferencePlot_Appropriate.pdf", width = 6, height = 4, units = "in", dpi=1200)

